package com.lunwen.lunwen.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lunwen.lunwen.bean.*;
import com.lunwen.lunwen.services.*;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import java.text.NumberFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * (MuBanRenWu)表控制层
 *
 * @author makejava
 * @since 2020-07-24 12:30:25
 */
@RestController
@RequestMapping("api")
public class MuBanRenWuController {

    /**
     * 服务对象
     */
    @Autowired
    private MuBanRenWuService muBanRenWuService;

    @Autowired
    private MuBanRenWuJsonService muBanRenWuJsonService;

    @Autowired
    private XueShengService xueShengService;

    @Autowired
    private MuBanService muBanService;

    @Autowired
    private MuBanRenWuRiZhiService muBanRenWuRiZhiService;

    @RequestMapping("insertmubanrenwu")
    public Result insertMuBanRenWu(@RequestBody CanShu1 canShu1){
        Result result = new Result();
        MuBanRenWu muBanRenWu = new MuBanRenWu();
        MuBanRenWuJson muBanRenWuJson = new MuBanRenWuJson();
        muBanRenWu.setMubanrenwumingcheng(canShu1.getCanshu1());
        try {
            muBanRenWu.setXueshengId(Long.valueOf(canShu1.getCanshu2().trim()));
        } catch (NumberFormatException e){
            result.setResult("201","学生ID格式错误！");
            return result;
        }
        muBanRenWu.setMubanId(canShu1.getCanshu3());
        muBanRenWu.setShengchengwenjian(canShu1.getCanshu4());
        muBanRenWu.setXiaoxi(canShu1.getCanshu5());
        muBanRenWu.setFuwuqi(canShu1.getCanshu6());
        muBanRenWu.setLeixing(canShu1.getCanshu7());
        muBanRenWu.setCanshu1(canShu1.getCanshu8());
        muBanRenWu.setCanshu2(canShu1.getCanshu9());
        muBanRenWu.setCanshu3(canShu1.getCanshu10());
        muBanRenWu.setCanshu4(canShu1.getCanshu11());
        muBanRenWu.setCanshu5(canShu1.getCanshu12());
        muBanRenWu.setCanshu6(canShu1.getCanshu13());
        muBanRenWu.setCanshu7(canShu1.getCanshu14());
        muBanRenWu.setCanshu8(canShu1.getCanshu15());
        muBanRenWu.setCanshu9(canShu1.getCanshu16());
        muBanRenWu.setCanshu10(canShu1.getCanshu17());
        if (StringUtils.isEmpty(muBanRenWu.getMubanrenwumingcheng())){
            result.setResult("201","模板名称不能为空！");
            return result;
        }
        if (muBanRenWu.getXueshengId() == null){
            result.setResult("201","学生ID不能为空！");
            return result;
        }
        if (StringUtils.isEmpty(muBanRenWu.getMubanId())){
            result.setResult("201","模板ID不能为空！");
            return result;
        }
        if (xueShengService.queryByUserId(muBanRenWu.getXueshengId()) == null){
            result.setResult("201","该学生不存在！");
            return result;
        }
        if (muBanService.queryById(muBanRenWu.getMubanId()) == null){
            result.setResult("201","该模板不存在！");
            return result;
        }

        muBanRenWu.setMubanrenwuId(UUID.randomUUID().toString());
        muBanRenWu.setTijiaoshijian(new Date());
        muBanRenWu.setZhuangtai(1);

        muBanRenWuJson.setMubanrenwuId(muBanRenWu.getMubanrenwuId());
        System.out.println(JSONObject.toJSONString(muBanRenWu,SerializerFeature.WriteMapNullValue));
        muBanRenWuJson.setMubanrenwuJson(JSONObject.toJSONString(muBanRenWu,SerializerFeature.WriteMapNullValue));
        muBanRenWuJson.setShijian(new Date());
        muBanRenWuJson.setZhuangtai(muBanRenWu.getZhuangtai());

        MuBanRenWuJson insert = muBanRenWuJsonService.insert(muBanRenWuJson);
        result.setResult("200","添加成功！",insert.getMubanrenwuJson());
        return result;
    }

    @RequestMapping("selectfirstmubanrenwu")
    public Result selectFirstMuBanRenWu(@RequestBody CanShu1 canShu1){
        Result result = new Result();
        Integer zhuangtai = null;
        try {
            zhuangtai = Integer.valueOf(canShu1.getCanshu1().trim());
        } catch (NumberFormatException e) {
            result.setResult("201","状态格式错误！");
            return result;
        }
        if (zhuangtai != 1 && zhuangtai != 2 && zhuangtai != 3 && zhuangtai !=4){
            result.setResult("201","状态码错误！");
            return result;
        }
        List<MuBanRenWuJson> muBanRenWuJsons = muBanRenWuJsonService.queryAllByLimit(zhuangtai, 0, 1);
        if (muBanRenWuJsons != null){
            if (muBanRenWuJsons.size() > 0){
                result.setResult("200","查询成功！",muBanRenWuJsons.get(0).getMubanrenwuJson());
                return result;
            }
        }
        result.setResult("201","查询失败！");
        return result;
    }

    @RequestMapping("updatemubanrenwuzhuangtai")
    public Result updateMuBanRenWuZhuangTai(@RequestBody CanShu1 canShu1){
        Result result = new Result();
        String id = canShu1.getCanshu8();
        Integer zhuangtai = null;
        try {
            zhuangtai = Integer.valueOf(canShu1.getCanshu9().trim());
        } catch (NumberFormatException e) {
            result.setResult("201","状态格式错误！");
            return result;
        }
        MuBanRenWu muBanRenWu = new MuBanRenWu();
        MuBanRenWuJson muBanRenWuJson = new MuBanRenWuJson();
        muBanRenWu.setMubanrenwumingcheng(canShu1.getCanshu1());
        try {
            muBanRenWu.setXueshengId(Long.valueOf(canShu1.getCanshu2().trim()));
        } catch (NumberFormatException e){
            result.setResult("201","学生ID格式错误！");
            return result;
        }
        muBanRenWu.setMubanId(canShu1.getCanshu3());
        muBanRenWu.setShengchengwenjian(canShu1.getCanshu4());
        muBanRenWu.setXiaoxi(canShu1.getCanshu5());
        muBanRenWu.setFuwuqi(canShu1.getCanshu6());
        muBanRenWu.setLeixing(canShu1.getCanshu7());
        muBanRenWu.setMubanrenwuId(id);
        muBanRenWu.setZhuangtai(zhuangtai);

        muBanRenWu.setCanshu1(canShu1.getCanshu10());
        muBanRenWu.setCanshu2(canShu1.getCanshu11());
        muBanRenWu.setCanshu3(canShu1.getCanshu12());
        muBanRenWu.setCanshu4(canShu1.getCanshu13());
        muBanRenWu.setCanshu5(canShu1.getCanshu14());
        muBanRenWu.setCanshu6(canShu1.getCanshu15());
        muBanRenWu.setCanshu7(canShu1.getCanshu16());
        muBanRenWu.setCanshu8(canShu1.getCanshu17());
        muBanRenWu.setCanshu9(canShu1.getCanshu18());
        muBanRenWu.setCanshu10(canShu1.getCanshu19());

        if (StringUtils.isEmpty(muBanRenWu.getMubanrenwumingcheng())){
            result.setResult("201","模板名称不能为空！");
            return result;
        }
        if (muBanRenWu.getXueshengId() == null){
            result.setResult("201","学生ID不能为空！");
            return result;
        }
        if (StringUtils.isEmpty(muBanRenWu.getMubanId())){
            result.setResult("201","模板ID不能为空！");
            return result;
        }
        if (xueShengService.queryByUserId(muBanRenWu.getXueshengId()) == null){
            result.setResult("201","该学生不存在！");
            return result;
        }
        if (muBanService.queryById(muBanRenWu.getMubanId()) == null){
            result.setResult("201","该模板不存在！");
            return result;
        }

        if (StringUtils.isEmpty(id)){
            result.setResult("201","模板任务ID不能为空！");
            return result;
        }
        if (zhuangtai != 1 && zhuangtai != 2 && zhuangtai != 3 && zhuangtai !=4){
            result.setResult("201","状态码设置错误！");
            return result;
        }
        if (muBanRenWuJsonService.queryById(muBanRenWu.getMubanrenwuId()) == null){
            result.setResult("201","模板任务不存在！");
            return result;
        }
        if (zhuangtai == 3){
            muBanRenWu.setWanchengshijian(new Date());
        }

        muBanRenWuJson.setMubanrenwuId(muBanRenWu.getMubanrenwuId());
        System.out.println(JSONObject.toJSONString(muBanRenWu,SerializerFeature.WriteMapNullValue));
        muBanRenWuJson.setMubanrenwuJson(JSONObject.toJSONString(muBanRenWu,SerializerFeature.WriteMapNullValue));
        muBanRenWuJson.setShijian(new Date());
        muBanRenWuJson.setZhuangtai(muBanRenWu.getZhuangtai());
        MuBanRenWuJson update = muBanRenWuJsonService.update(muBanRenWuJson);

        if (zhuangtai == 3 || zhuangtai == 4){
            MuBanRenWuRiZhi muBanRenWuRiZhi = new MuBanRenWuRiZhi();
            muBanRenWuRiZhi.setMubanrenwumingcheng(muBanRenWu.getMubanrenwumingcheng());
            muBanRenWuRiZhi.setXueshengId(muBanRenWu.getXueshengId());
            muBanRenWuRiZhi.setTijiaoshijian(muBanRenWu.getTijiaoshijian());
            muBanRenWuRiZhi.setWanchengshijian(muBanRenWu.getWanchengshijian());
            muBanRenWuRiZhi.setShengchengwenjian(muBanRenWu.getShengchengwenjian());
            muBanRenWuRiZhi.setXiaoxi(muBanRenWu.getXiaoxi());
            muBanRenWuRiZhi.setFuwuqi(muBanRenWu.getFuwuqi());
            muBanRenWuRiZhi.setZhuangtai(muBanRenWu.getZhuangtai());
            muBanRenWuRiZhi.setLeixing(muBanRenWu.getLeixing());
            MuBanRenWuRiZhi insert = muBanRenWuRiZhiService.insert(muBanRenWuRiZhi);
        }
        result.setResult("200","修改成功！",update.getMubanrenwuJson());
        return result;
    }



}